package org.crmsystem.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.dao.ProKindMapper;
import org.crmsystem.dao.ProductMapper;
import org.crmsystem.model.Product;
import org.crmsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/8.
 */
@Service
public class ProductServiceImpl implements IProductService{
    private static Logger logger = LogManager.getLogger(ProKindMapper.class);
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IProKindService proKindService;
    @Autowired
    private IOrgService orgService;
    @Autowired
    private IUserService userService;
    /**
     * <!--根据主键id删除产品-->
     *
     * @param id the id
     * @return the int
     */
   public int deleteByPrimaryKey(Integer id){
       return  productMapper.deleteByPrimaryKey(id);
   }



    /**
     *  <!--插入产品-->
     *
     * @param record the record
     * @return the int
     */
    public int inserts(Product record){
        record.getProName();
        int flag=productMapper.inserts(record);
        return flag;
    }

    /**
     * <!--根据主键id查找产品-->
     *
     * @param id the id
     * @return the product
     */
    public Product selectByPrimaryKey(Integer id){
        return productMapper.selectByPrimaryKey(id);
    }
    /**
     * <!--查找所有产品-->
     *
     * @param
     * @return the product
     */
    public List select(){
        List plist=new ArrayList();
        List<Product> list = productMapper.select();

        for (Product p:list
             ) {

            Map map=new HashMap();
           logger.info(p.getOrgId()+"pppp");
           logger.info(p.getId()+"ddddd");
           logger.info(p.getOperatorId()+"ddddd");
            map.put("id",p.getId());
            map.put("proState",p.getProState());
            map.put("useState",p.getUseState());
            map.put("useDate",p.getUseDate());
            map.put("kindName",proKindService.selectByPrimaryKey(p.getKindId()).getName());
            map.put("orgName",orgService.getOrgById(p.getOrgId()).getName());
            map.put("operatorId",userService.getUserById(p.getOperatorId()).getName());
            map.put("contactId",userService.getUserById(p.getContactId()).getName());
            map.put("proName",p.getProName());
            map.put("remarks",p.getRemarks());
            plist.add(map);
        }
        return plist;
    }

    /**
     * <!--多条件查找产品-->
     *
     * @param
     * @return the product
     */
    public List selectByTerms(Map record){
        logger.info(record.get("pageSize")+"eeee");
        List plist=new ArrayList();
        List<Product> list = productMapper.selectByTerms(record);
        for (Product p:list
                ) {
            Map map=new HashMap();
            map.put("id",p.getId());
            map.put("proState",p.getProState());
            map.put("useState",p.getUseState());
            map.put("useDate",p.getUseDate());
            map.put("kindName",proKindService.selectByPrimaryKey(p.getKindId()).getName());
            map.put("orgName",orgService.getOrgById(p.getOrgId()).getName());
            map.put("operatorId",userService.getUserById(p.getOperatorId()).getName());
            map.put("contactId",userService.getUserById(p.getContactId()).getName());
            map.put("proName",p.getProName());
            map.put("remarks",p.getRemarks());
            plist.add(map);
        }
        return plist;
    }
    /**
     * <!--查找产品总记录-->
     *
     * @param
     * @return the product
     */
    public int selectByTermsTnum(){
        return productMapper.selectByTermsTnum();
    }
    /**
     * <!--根据主键id选择性修改产品-->
     *
     * @param record the record
     * @return the int
     */
    public int updateByPrimaryKeySelective(Product record){
        return productMapper.updateByPrimaryKeySelective(record);
    }
}
